home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_02 / ED-157 / load_buffer.c < prev    next >
C/C++ Source or Header  |  1993-09-10  |  2KB  |  64 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "rec.h"
  25. #include "buffer.h"
  26.  
  27. /******************************************************************************\
  28. |Routine: load_buffer
  29. |Callby: edit word_fill
  30. |Purpose: Loads a buffer with the contents of a string, handling appearances
  31. |         of <cr> in the string by starting a new record in the buffer.
  32. |Arguments:
  33. |    buffer is the buffer that receives the record(s) from the string.
  34. |    string is the string that may contain <cr> characters.
  35. |    i is the length of the meaningful data in the string.
  36. \******************************************************************************/
  37. void load_buffer(buffer,string,i)
  38. buf_ptr buffer;
  39. Char *string;
  40. Int i;
  41. {
  42.     register Int j,l;
  43.     register rec_ptr r;
  44.  
  45.     buffer_empty(buffer);
  46.     string[i++] = '\r';
  47.     buffer->nrecs = 0;
  48.     for(l = j = 0;j < i;j++)
  49.         if(string[j] == '\r')
  50.         {
  51.             buffer->nrecs++;
  52.             r = (rec_ptr)imalloc(sizeof(rec_node));
  53.             r->data = (Char *)imalloc(j - l + 1);
  54.             if(j > l)
  55.                 memcpy(r->data,string + l,j - l);
  56.             r->data[j - l] = '\0';
  57.             r->length = j - l;
  58.             r->recflags = 1;    /* it is a freeable buffer */
  59.             insq(r,buffer->last);
  60.             l = j + 1;
  61.         }
  62. }
  63.  
  64.